package lumis.portal.presentation.tag.page;

import lumis.portal.PortalContext;
import lumis.portal.PortalException;
import lumis.portal.PortalRequestParameters;
import lumis.portal.PortalStringResource;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.PageConfig;
import lumis.portal.presentation.core.HTMLFragmentComponent;
import lumis.portal.presentation.core.LayoutFilePage;
import lumis.util.IResource;
import lumis.util.TextUtil;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.util.string.AppendingStringBuffer;

/**
 * Injects the page's body footer component.
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class PageBodyFooterComponent extends HTMLFragmentComponent 
{
	/**
	 * Serial version identifier.
	 * @since 6.0.0
	 */
	private static final long serialVersionUID = 1L;

	public PageBodyFooterComponent(String id, boolean autoRenderBodyOnly) 
	{
		super(id, autoRenderBodyOnly);
	}

	@Override
	protected CharSequence getHTMLFragment(MarkupStream markupStream, ComponentTag openTag) 
	{
		try
		{
			AppendingStringBuffer bodyFooter = new AppendingStringBuffer(1500);
			SessionConfig sessionConfig = SessionConfig.getCurrentSessionConfig();
			LayoutFilePage page = ((LayoutFilePage)getPage());
			PageConfig pageConfig = page.getPageWebResource().getPage();

			if (page.getGeneratorPageCacheId() != null)
			{
				IResource resource = new PortalStringResource();
				if (PortalContext.getHtmlGenerationConfig().isHtmlCacheLogNavigation())
				{
					bodyFooter.append("<div style=\"display:none\"><img src=\"lumis/portal/controller/html/NavigationLogger.jsp?" + PortalRequestParameters.PAGE_PARAMETER_PAGEID + "=" + pageConfig.getId() + "\" title=\""+ManagerFactory.getLocalizationManager().localize("STR_LOG_NAVIGATION", resource, sessionConfig.getLocale())+"\" alt=\""+ManagerFactory.getLocalizationManager().localize("STR_LOG_NAVIGATION", resource, sessionConfig.getLocale())+"\"/></div>");
				}
				String html = "<div style=\"display:none\"><img src=\"lumis/portal/controller/html/SetLocale.jsp?lumUserLocale="+sessionConfig.getLocale()+"\" title=\""+ManagerFactory.getLocalizationManager().localize("STR_SET_LOCALE", resource, sessionConfig.getLocale())+"\" alt=\""+ManagerFactory.getLocalizationManager().localize("STR_SET_LOCALE", resource, sessionConfig.getLocale())+"\"/></div>";
				bodyFooter.append("<script type=\"text/javascript\">\n//<![CDATA[\n if(document.cookie.search(\"lumUserLocale=\")==-1)document.write(\"" + TextUtil.escapeJs(html) + "\");\n//]]>\n</script><noscript><!--Lumis portal script--></noscript>");
			}

			if (page.isPrinting())
				bodyFooter.append("<script type=\"text/javascript\">window.print();</script><noscript><!--Lumis portal script--></noscript>");
			
			return bodyFooter;
		}
		catch (PortalException e)
		{
			throw new RuntimeException(e);
		}
	}
}
